How to use Random Numbers in NumPy?
41
25-Nov-2025
Updated on 25-Nov-2025
Anubhav Kumar
25-Nov-2025NumPy Random Numbers
NumPy provides random number functions through:
There are many ways to generate random numbers:
1. Random Integer (
randint)Generate multiple random integers:
2. Random Floats (
rand,randn,random)Uniform distribution (0 → 1)
Multi-dimensional:
Normal distribution (mean=0, std=1)
General random floats
3. Random Choice (
choice)Select random values from a list or array:
Select multiple:
With replacement disabled:
4. Random Shuffle
Shuffle array in-place:
5. Random Permutation (returns new array)
6. Random Seed (reproduce results)
To get same random numbers every time:
If you run again with seed=42, output will match exactly.
7. Random distribution samples
Normal distribution with custom mean & std
Binomial distribution
Poisson distribution
8. New Generator API (recommended)
NumPy recommends using:
Example:
Summary Table
randint,integersrand,randomrandn,normalchoiceshuffle,permutationseeddefault_rng()